package com.test.deencredit.adapter_deencredit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.deencredit.R;
import com.test.deencredit.view_deencredit.NumberTextView_L_deencredit;

import java.util.ArrayList;


public class HomeCarouselAdapter_deencredit extends RecyclerView.Adapter<HomeCarouselAdapter_deencredit.HomeCarouselHolder_deencredit> {

    private final Context mContext_deencredit;
    private final ArrayList<HomeCarouseInfo_deencredit> infos_deencredit;

    public HomeCarouselAdapter_deencredit(Context context, ArrayList<HomeCarouseInfo_deencredit> infos){
        mContext_deencredit = context;
        this.infos_deencredit = infos;
    }

    @NonNull
    @Override
    public HomeCarouselHolder_deencredit onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HomeCarouselHolder_deencredit(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_carousel_deencredit,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull HomeCarouselHolder_deencredit holder, int position) {

        int size = infos_deencredit.size();
        int index=position%size;

        HomeCarouseInfo_deencredit info = infos_deencredit.get(index);

        holder.setAmount_deencredit(info.getAmount_deencredit());
        holder.initPointGroup_deencredit(index,size);
        holder.setLock_deencredit(info.isLock_deencredit());

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public static class HomeCarouselHolder_deencredit extends RecyclerView.ViewHolder{

        private final View mView;

        private final NumberTextView_L_deencredit numberTextViewL_deencredit;
        private final LinearLayout pointgroupLayout_deencredit;
        private final View lockView_deencredit;

        @SuppressLint("InflateParams")
        public HomeCarouselHolder_deencredit(View view){
            super(view);
            mView = view;

            numberTextViewL_deencredit = mView.findViewById(R.id.ihc_view_number_text);
            pointgroupLayout_deencredit = mView.findViewById(R.id.ihc_layout_point_group);
            lockView_deencredit = mView.findViewById(R.id.ihc_view_lock);
        }

        public void setAmount_deencredit(CharSequence amount){
            numberTextViewL_deencredit.setText_deencredit(amount);
        }

        public void initPointGroup_deencredit(int index, int max){
            pointgroupLayout_deencredit.removeAllViews();
            for (int i=0;i<max;i++){
                View pointView = LayoutInflater.from(mView.getContext()).inflate(R.layout.view_blank_deencredit,null);
                pointView.setBackgroundResource(index==i ? R.drawable.bg_home_carousel_point_true_deencredit :R.drawable.bg_home_carousel_point_false_deencredit);
                pointgroupLayout_deencredit.addView(pointView);
            }
        }

        public void setLock_deencredit(boolean isLock){
            lockView_deencredit.setVisibility(isLock ? View.VISIBLE:View.GONE);
        }
    }

    public static class HomeCarouseInfo_deencredit {

        private final String amount_deencredit;
        private final boolean isLock_deencredit;

        public HomeCarouseInfo_deencredit(String amount, boolean isLock) {
            this.amount_deencredit = amount;
            this.isLock_deencredit = isLock;
        }

        public String getAmount_deencredit() {
            return amount_deencredit;
        }

        public boolean isLock_deencredit() {
            return isLock_deencredit;
        }
    }
}
